<?
include_once("../config.php");
//获取openid的顺序是要用code换的，不是用accesstoken。


//请确保表格存在，并且公众号数据在nid=1的位置
$wechat_detail=select_one_line_all_items($table_prefix."wechat_official","nid = 1");


//配置参数的数组
$CONF =  array(
    '__APPID__' =>$wechat_detail['appid'],
    '__SERECT__' =>$wechat_detail['appsecret'],
    '__CALL_URL__' =>'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
);



//没有传递code的情况下，先登录一下获取code。
if(!isset($_GET['code']) || empty($_GET['code'])){

    $getCodeUrl  =  "https://open.weixin.qq.com/connect/oauth2/authorize".
        "?appid=" . $CONF['__APPID__'] .
        "&redirect_uri=" . $CONF['__CALL_URL__']  .
        "&response_type=code".
        "&scope=snsapi_base". #!!!scope设置为snsapi_base !!!
        "&state=1";

//跳转微信获取code值,去登陆
    header('Location:' . $getCodeUrl);
    exit;
}



//经过上面的步骤，这里必然会有code

$code     		=	trim($_GET['code']);
//使用code，拼凑链接获取用户openid
$getTokenUrl    =  "https://api.weixin.qq.com/sns/oauth2/access_token".
    "?appid={$CONF['__APPID__']}".
    "&secret={$CONF['__SERECT__']}".
    "&code={$code}".
    "&grant_type=authorization_code";


$get_json=file_get_contents($getTokenUrl);
//var_dump($get_json);
$json = json_decode($get_json,true);


if (!empty($json['errcode'])) {
    echo '<script type="text/javascript">alert("当前获取微信状态有误，请重新尝试；错误代码：'.$json['errcode'].'")</script>';
    return false;
}

$wx_user_token_arr = json_decode($get_json,true);
//print_r2($wx_user_token_arr);
//通过获取到的openid 来读取微信用户信息
$openid=$wx_user_token_arr['openid'];

//直到这里，获取openid完毕
?>






<?
echo "开始执数据";

//需要openid绑定员工账号。识别码确定公司；


print_r2($_REQUEST);
$p=explode("@",$_REQUEST['p']);


print_r2($p);

include_once ("../connection.php");


echo "<br/>";

$_p['ai_openid']=$openid;

echo $_bind=update_date($table_prefix."employee",$_p,"nid",$p['0']);
$query=do_query($_bind) or die ("error:".mysql_error());


$wechat_detail2=select_one_line_all_items($table_prefix."wechat_official","nid = 1");

print_r2($wechat_detail2);



$_fin_url="https://milkorder.dagger.enpingshi.com/front/milkorder/view/binding_employee_wechat_finished.php";
header('Location:' . $_fin_url);



?>